//
//  NovelViewController.m
//  A_Program
//
//  Created by dlios on 15-5-21.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "NovelViewController.h"
#import "SANetWorkingTask.h"
#import "NovelSecondModel.h"
#import "NovelFristModel.h"
#import "NovelFristCollectionViewCell.h"
#import "NovelSecondCollectionViewCell.h"
#import "NovelSecondModel.h"
#import "NovelDetailsViewController.h"
#import "NovelFristImageview.h"
#import "NovelAlbumViewController.h"
#import <Reachability.h>
@interface NovelViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,NovelFristCollectionDelegate>

@end

@implementation NovelViewController
- (void)dealloc
{
    [self.fristModelArray release];
    [self.secondModelArray release];
    [self.myCollectionView release];
    [self.headerView release];
    [self.contentView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        }
    return self;
    }

- (void)loadView{
    [super loadView];
    self.view.backgroundColor = [UIColor clearColor];
    //设置题头
    self.navigationItem.title = @"有声小说";
    self.navigationController.navigationBar.translucent = NO;
    //设置
    self.tabBarController.tabBar.translucent = NO;
    //
    [self.view setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:0.9]];
    [self createCollectionView];
    [self createHeaderView];
    if ([self isConnectionAvailable]) {
        [self createFristData];
        [self createSecondData];

    }
    
}
//判断网络使用
-(BOOL) isConnectionAvailable{
    
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            NSLog(@"没网");
            [self alertView:@"没有网络连接"];
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            NSLog(@"用的WIFI");
                break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            NSLog(@"3G");
            [self alertView:@"使用3G连接"];
            break;
    }
    
    
    return isExistenceNetwork;
}
// 警告框
- (void)alertView:(NSString *)str
{
    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];
    [alert setDelegate:self];
    [alert show];
    [alert release];
    
}




- (void)createFristData{
    self.fristModelArray = [NSMutableArray array];
    NSMutableArray *myFristArray = [NSMutableArray array];
    //第一个数据
    NSString *firstPath = @"http://mobile.ximalaya.com/m/category_focus_image?categoryId=3&version=3.25.7.1&device=android";
    [SANetWorkingTask request:firstPath parmater:nil block:^(NSDictionary *result) {
        NSMutableDictionary *dic = (NSMutableDictionary *)result;
        NSMutableArray *array = [dic objectForKey:@"list"];
        for (NSMutableDictionary *modelDic in array) {
            NovelFristModel *firstModel = [[NovelFristModel alloc]init];
            [firstModel setValuesForKeysWithDictionary:modelDic];
            [myFristArray addObject:firstModel];
        }
        self.fristModelArray = [NSMutableArray arrayWithArray:myFristArray];
        [self.myCollectionView reloadData];
        [self.headerView createViewData:self.fristModelArray];
    }];

}

- (void)createSecondData{
    //第二个数据
    self.secondModelArray = [NSMutableArray array];
    NSMutableArray *mySecondArray = [NSMutableArray array];
    NSString *secondPath = @"http://mobile.ximalaya.com/m/category_tag_list?device=android&per_page=100&category=book&type=album&page=1";
    [SANetWorkingTask request:secondPath parmater:nil block:^(NSDictionary *result) {
        NSMutableDictionary *dic = (NSMutableDictionary *)result;
        NSMutableArray *array = [dic objectForKey:@"list"];
        for (NSMutableDictionary *modelDic in array) {
            NovelSecondModel *secondModel = [[[NovelSecondModel alloc]init]autorelease];
            [secondModel setValuesForKeysWithDictionary:modelDic];
            [mySecondArray addObject:secondModel];
        }
        self.secondModelArray = [NSMutableArray arrayWithArray:mySecondArray];
        [self.myCollectionView reloadData];
        
        
    }];

}


- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //相对上下左右的设置
    layout.sectionInset = UIEdgeInsetsMake([UIScreen mainScreen].bounds.size.height/4 + 10,10, 10, 10);
    //上下cell之间的距离改变
    layout.minimumLineSpacing = 10.0;
    //左右cell之间的距离改变
    layout.minimumInteritemSpacing = 5.0;
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height *0.835)collectionViewLayout:layout];
    //间隙颜色部分
    self.myCollectionView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [self.view addSubview:self.myCollectionView];
    
    //cell的重用池注册
    [self.myCollectionView registerClass:[NovelSecondCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
    [layout release];
    [self.myCollectionView release];
    
}


//在collection中加入滚动图
-(void)createHeaderView{
    self.headerView = [[[NovelFristCollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/4)]autorelease];
    [self.myCollectionView addSubview:self.headerView];
    self.headerView.novelCollectionDelegate = self;
    
    [self.headerView release];
}
//header图片手势跳转
- (void)createNovelFristImageView:(NovelFristImageView *)novelFristImageview{
    NovelFristModel *novelFristModel = novelFristImageview.novelFristModel;
    NovelAlbumViewController *albumView = [[NovelAlbumViewController alloc]init];
    albumView.publicPath = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/15",novelFristModel.myId];
    [self.navigationController pushViewController:albumView animated:YES];
    [albumView release];
}

#pragma mark -
#pragma mark collectionView协议部分
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

        return [self.secondModelArray count];
}

#pragma mark -
#pragma mark cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

        self.contentView = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        NovelSecondModel *secondModel = [self.secondModelArray objectAtIndex:indexPath.row];
        [self.contentView createContentImageView:secondModel];
        
        return self.contentView;

}
//设置cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        return CGSizeMake(([UIScreen mainScreen].bounds.size.width-40)/3,([UIScreen mainScreen].bounds.size.height-60)/5.5);
}
//跳转页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NovelSecondModel *secondModel = [self.secondModelArray objectAtIndex:indexPath.row];
    NovelDetailsViewController *detailsView = [[NovelDetailsViewController alloc]init];
        NSLog(@"%@",secondModel.tname);
    detailsView.namePath = secondModel.tname;
    [self.navigationController pushViewController:detailsView animated:YES];
    [detailsView release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
